<script>
    jxt.table.newJson = function(dataJson, attrs) {
        var ret = [];
        if (!dataJson) dataJson = jxt.table.dataJson;
        var _self = jxt.tag.newJson(dataJson);
        _self.children = [];
        var row;
        var column;
        if (attrs) {
            row = attrs.row;
            column = attrs.column;
        }

        if (!row) row = dataJson.init.rowcount;
        if (!column) column = dataJson.init.columncount;

        for (var i = 0; i < row; i++) {
            var r = [];
            for (var j = 0; j < column; j++) {
                var _text = jxt.text.newJson();
                ret.push(_text);
                r.push([_text.id]);
            }
            _self.children.push(r);
        }
        ret.splice(0, 0, _self);
        return ret;

    };
    jxt.table.help_callback=  jxt.table._add_ = function (subscribe_id, eventkey, attrs, event_source_id) {

        switch (eventkey){
            case "help_img":
                jxt.table.add(subscribe_id, "img", attrs);
                break;
            case "help_file":
                jxt.table.add(subscribe_id, "file", attrs);
                break;
            case "help_link":
                jxt.table.add(subscribe_id, "link", attrs);
                break;
        }
    };
    jxt.table.add = function(eleid, tagname,attrs) {
        var _eleid = eleid;
        var node = jxt.getnode(_eleid);
        var newnode = jxt.newJson(tagname,attrs);
        var at_id = jxt.tag.getNowEditEle(eleid);
        node = jxt.table._add(node, newnode, at_id);

        jxt.msg.radio("table", "addchildnode", {"newnodelist": newnode});
        jxt.msg.radio(eleid, "nodechange");
        jxt.render(eleid, true);
    };

    jxt.table._add = function(node, newnode, at_id) {
        if (!node.children) {
            node.children = [
                []
            ];
            node.children.push([newnode.id]);
            return node;
        }
        var b = false;
        if (at_id) {
            var rows = node.children;
            for (var i = 0; i < rows.length; i++) {
                var cells = rows[i];
                for (var j = 0; j < cells.length; j++) {
                    var cell = cells[j];
                    for (var k = 0; k < cell.length; k++) {
                        if (at_id == cell[k]) {
                            node.children[i][j].splice(k + 1, 0, newnode.id);
                            b = true;
                            break;
                        }
                    }
                    if (b) break;
                }
                if (b) break;
            }
        }
        if (!b) {
            node.children[0][0].push(newnode.id);
        }
        return node;
    };

    jxt.table.addrow = function(eleid) {
        var n = jxt.getnode(eleid);
        var at_id = jxt.tag.getNowEditEle(eleid);
        var ijk = jxt.table._getIJK(n, at_id);

        var columncount = n.children[0].length;
        var r = [];
        for (var i = 0; i < columncount; i++) {
            var _text = jxt.newJson("text");
            jxt.addNode(_text);
            r.push([_text.id]);
        }

        n.children.splice(ijk.i + 1, 0, r);
        jxt.msg.radio(eleid, "nodechange");
        jxt.render(eleid, true);
    };

    jxt.table._getIJK = function(n, at_id) {
        var b = false;
        var ii = -1;
        var jj = -1;
        var kk = -1;
        if (at_id) {
            var rows = n.children;
            for (var i = 0; i < rows.length; i++) {
                var cells = rows[i];
                for (var j = 0; j < cells.length; j++) {
                    var cell = cells[j];
                    for (var k = 0; k < cell.length; k++) {
                        if (at_id == cell[k]) {
                            ii = i;
                            jj = j;
                            kk = k;
                            b = true;
                            break;
                        }
                    }
                    if (b) break;
                }
                if (b) break;
            }

        }
        if (ii == -1) {
            ii = n.children.length - 1;
            jj = n.children[ii].length - 1;
            kk = n.children[ii][jj].length - 1;
        }
        return {
            i: ii,
            j: jj,
            k: jj
        };
    };

    jxt.table.delrow = function(eleid) {
        var n = jxt.getnode(eleid);
        var at_id = jxt.tag.getNowEditEle(eleid);
        var ijk = jxt.table._getIJK(n, at_id);
        n.children.splice(ijk.i, 1);

        jxt.msg.radio(eleid, "nodechange");
        jxt.render(eleid, true);

    };

    jxt.table.addcolumn = function(eleid) {
        var n = jxt.getnode(eleid);
        var at_id = jxt.tag.getNowEditEle(eleid);
        var ijk = jxt.table._getIJK(n, at_id);

        var rowcount = n.children.length;

        for (var i = 0; i < rowcount; i++) {
            var _text = jxt.newJson("text");
            jxt.addNode(_text);
            n.children[i].splice(ijk.j + 1, 0, [_text.id]);
        }

        jxt.msg.radio(eleid, "nodechange");
        jxt.render(eleid, true);
    };
    jxt.table.delcolumn = function(eleid) {
        var n = jxt.getnode(eleid);
        var at_id = jxt.tag.getNowEditEle(eleid);
        var ijk = jxt.table._getIJK(n, at_id);
        var rowcount = n.children.length;
        for (var i = 0; i < rowcount; i++) {
            n.children[i].splice(ijk.j, 1);
        }
        jxt.msg.radio(eleid, "nodechange");
        jxt.render(eleid, true);
    };





    jxt.table.create = function (subscribe_id, eventkey, attrs, event_source_id) {
        var node = jxt.table.newJson();
        if(attrs && attrs.tpname)
        {
            node[0].tpname=attrs.tpname;
        }
        jxt.msg.radio("table", "addmainnode", {"newnodelist": node});
        //jxt.route.getFunc('table.editbegin')(node[0].id);
        jxt.route.exe('table.editbegin',null,node[0].id);
    };
  /*  jxt.table.help_callback=function (subscribe_id, eventkey, attrs, event_source_id) {
        jxt.table._add_(subscribe_id, eventkey, attrs, event_source_id);
    };*/
    jxt.msg.subscribe("table", "createtable", jxt.table.create);
</script>